<template>
  <div>
    <el-dialog
      title="添加成员"
      :visible.sync="dialogFormVisible"
      @close="handleClose()"
    >
      <el-form
        :model="form"
        :rules="rules"
        status-icon
        ref="form"
        v-loading="loading"
      >
        <el-form-item label="姓名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex" :label-width="formLabelWidth">
          <el-radio v-model="form.sex" :label="0">男</el-radio>
          <el-radio v-model="form.sex" :label="1">女</el-radio>
        </el-form-item>
        <el-form-item label="状态" prop="status" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-text="开启"
            inactive-text="关闭"
          ></el-switch>
        </el-form-item>

        <el-form-item label="角色" prop="rule" :label-width="formLabelWidth">
          <el-input v-model="form.rule" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="create('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CreateComp",
  components: {},
  data() {
    return {
      loading: false,
      dialogFormVisible: false,
      form: {
        name: "",
        sex: 0,
        status: true,
        rule: "",
      },
      rules: {
        name: { required: true, message: "请输入姓名", trigger: "blur" },
        rule: { required: true, message: "请输入职位", trigger: "blur" },
      },
      formLabelWidth: "120px",
    };
  },
  mounted() {},
  methods: {
    //提交
    create(form) {
      this.loading = true;

      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$emit("handleCreate", this.form);
          this.$message.success(`添加${this.form.name}成功！`);
        } else {
          this.$message.error("添加失败");
          return false;
        }
      });
    },
    //取消
    handleClose() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style scoped lang="scss"></style>
